//
//  JEHThreePicTableViewCell.m
//  SinaNews
//
//  Created by 蒋恩浩 on 15/12/3.
//  Copyright © 2015年 JEH. All rights reserved.
//

#import "JEHThreePicTableViewCell.h"

@interface JEHThreePicTableViewCell ()

@property (nonatomic, retain) UILabel *replyLabel;

@end

@implementation JEHThreePicTableViewCell

- (void)dealloc
{
    [_rootModel release];
    [_titleLabel release];
    [_replyLabel release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    for (int i = 0; i < 3; i++) {
        
        UIImageView *pictureView = (UIImageView *)[self viewWithTag:100 + i];
        pictureView.frame = CGRectMake(10 + (10 + (self.width - 40) / 3) * i, self.height / 4, (self.width - 40) / 3, self.height / 2 + 10);
    }
    self.titleLabel.frame = CGRectMake(10, 0, self.width, self.height / 4);
    CGFloat replyWidth = [self getWidth];
    self.replyLabel.frame = CGRectMake(self.width - 10 - replyWidth, self.height * 3 / 4 + 10, replyWidth, self.height / 4);
}

- (CGFloat)getWidth {
    NSDictionary *dic = @{NSFontAttributeName: [UIFont systemFontOfSize:15]};
    CGRect rect = [self.replyLabel.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, self.height * 3 / 4) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.width;
}

- (void)setRootModel:(JEHRootModel *)rootModel {
    if (_rootModel != rootModel) {
        [_rootModel release];
        _rootModel = [rootModel retain];
    }
    for (int i = 0; i < 3; i++) {
        if (i < rootModel.picsModel.listArray.count) {
            
            UIImageView *pictureView = (UIImageView *)[self viewWithTag:100 + i];
            JEHSecondModel *secondModel = rootModel.picsModel.listArray[i];
            [pictureView sd_setImageWithURL:[NSURL URLWithString:secondModel.kpic] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
            pictureView.contentMode = UIViewContentModeScaleAspectFill;
            pictureView.clipsToBounds = YES;
        }
    }
    self.titleLabel.text = rootModel.title;
    self.replyLabel.text = [NSString stringWithFormat:@"%ld评论", (long)rootModel.picsModel.total];
}

- (void)createView {
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [_titleLabel release];
    _titleLabel.font = [UIFont systemFontOfSize:15];
    self.replyLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [_replyLabel release];
    _replyLabel.font = [UIFont systemFontOfSize:10];
    _replyLabel.textColor = [UIColor grayColor];
    _replyLabel.textAlignment = NSTextAlignmentRight;
    for (int i = 0; i < 3; i++) {
        
        UIImageView *pictureView = [[UIImageView alloc] initWithFrame:CGRectZero];
        pictureView.tag = 100 + i;
        [self addSubview:pictureView];
        [pictureView release];
    }
    [self addSubview:self.titleLabel];
    [self addSubview:self.replyLabel];
    
#pragma mark - NightMode
    NSNotificationCenter *centers = [NSNotificationCenter defaultCenter];
    [centers addObserver:self selector:@selector(nightMode:) name:@"NightMode" object:nil];
}

- (void)nightMode:(NSNotification *)notification {
    
    if ([notification.object isEqualToString:@"YES"]) {
        
        for (int i = 0; i < 3; i++) {
            
            UIImageView *pictureView = (UIImageView *)[self viewWithTag:100 + i];
            pictureView.alpha = 0.7;
        }
        self.titleLabel.textColor = [UIColor whiteColor];
    } else {
        for (int i = 0; i < 3; i++) {
            
            UIImageView *pictureView = (UIImageView *)[self viewWithTag:100 + i];
            pictureView.alpha = 1;
        }
        self.titleLabel.textColor = [UIColor blackColor];
    }
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
